define(function(require, exports) {
	var pubjs = require('pubjs');
	var $ = require('jquery');
	var view  = require('@base/view');

	// 标尺控件
	var Base = view.container.extend({
		init: function(config) {
			config = pubjs.conf(config, {
				'width': '100%',
				'class': 'M-commonRuler',
				'bottom': 0,
				'left': 0,
				'space': 1
			});

			this.Super('init', arguments);
		},
		afterBuild: function() {
			var el = this.getDOM();
			var c = this.getConfig();

			el.css({
				'width':c.width,
				'bottom':c.bottom,
				'left':c.left
			}).html(_getRuleStr({
				'suffix':'0',
				'space': c.space
			}));
		}
	});

	//标尺生成主函数
	function _getRuleStr (config) {
		config = $.extend({
			'deep':1,//递归层级,为0则停止递归
			'count':10,//标尺刻度分段
			'prefix':'',//标尺刻度前缀
			'suffix':'',//刻度后缀
			'noDegree':false,//刻度显示控制
			'isEnd':true,//标尺末端控制，为true则为其添加0跟最终刻度
			'space':1,//刻度显示间距
			'curStyle':0//刻度数值样式大小控制,根据数值选择不同样式方案
		}, config)
		if(!config.deep){
			return '';
		}
		var d = config.deep,count = config.count,prefix = config.prefix,t,
		suffix = config.suffix,end;
		d--;
		var width = 100/count,arr = [],str = '',w = width+'%',space = config .space,
		c = {
			'deep':d,
			'count':count,
			'prefix':prefix,
			'suffix':suffix,
			'isEnd':false,
			'curStyle':config.curStyle+1
		};
		var sizeList = [
			{
				linesize:9,countsize:13
			},
			{
				linesize:5,countsize:8
			}
		],size = sizeList[config.curStyle];
		for(var i = 0;i<count;i++){
			end = '';
			if(i !== 0){
				t = config.noDegree?null:prefix+i+suffix;
				if(i%space !== 0){
					t = null
				}
			}
			else if(i===0){
				t = config.isEnd?0:null;
			}
			if(i===count-1&&config.isEnd){
				end = _buildCur(prefix+(i+1)+suffix,null,null,true);
			}
			str = '<span class="ruler" style="width:'+w+';">'+_buildCur(t,size.linesize,size.countsize)+_getRuleStr(c)+end+'</span>';
			arr.push(str);
		}
		return arr.join('');
	}
	/**
	 * 刻度生成函数
	 * @param  {string} t         刻度数值
	 * @param  {number} linesize  [刻度线条高度]
	 * @param  {number} countsize [数值高度]
	 * @param  {boolean} offRight  [是否生成末端刻度]
	 * @return {string}           [返回的html字段]
	 */
	function _buildCur(t,linesize,countsize,offRight){
		linesize = linesize||9;
		countsize = countsize||13;
		var off,str;
		if(offRight){
			off = 'right: '+(-countsize)+'px;'
		}
		else{
			off = 'left:'+(-(Math.max(linesize,countsize)/4+2))+'px;'
		}
		if(t === null){
			str = '';
		}
		else{
			str = '<div class="curCon" style="'+off+'"><p><span class="line" style="height:'+linesize+'px;"></span></p><p style="font-size:'+countsize+'px;">'+t+'</p></div>';
		}
		return str;
	}

	exports.base = Base;
});